using MATLAB
# 定义nrCarrierConfig结构体
struct nrCarrierConfig
    NCELLID::Float64
    NSizeGrid::Float64
    NStartGrid::Float64
    NSlot::Float64
    NFrame::Float64
    IntraCellGuardBands::Matrix{Float64}
    SymbolsPerSlot::Float64
    SlotsPerSubframe::Float64
    SlotsPerFrame::Float64
    SubcarrierSpacing::Float64
    CyclicPrefix::String
end
# 创建nrCarrierConfig实例并赋初值
carrier = nrCarrierConfig(
    1,
    51,
    0,
    199,
    0,
    zeros(Float64, 0, 2),
    14,
    2,
    20,
    30,
    "normal"
)
# 定义结构体
struct TransmissionConfig
    PrecodingMethod::String
    ScheduledLayers::Float64
    PerfectChannelEstimator::Float64
end

# 初始化结构体实例
alg = TransmissionConfig("RZF", 8, 1)

# 定义结构体类型
struct rxDLType
    rxWaveform
    rxGrid
    ofdmInfo
    pathGains
    sampleTimes
    noisePower
    ChannelFilterDelay
    pathFilters
end

# 由于是1x0的结构体，先创建一个空的数组来存储实例
rx = rxDLType[]

struct schedule
    PDSCH::Matrix{Float64}
    PUSCH::Vector{Float64}
    SRS::Matrix{Float64}
end
# 初始化 schedule 结构体实例
function initialize_scheduleConfig()
    PDSCH = zero(Float64,1,0)
    PUSCH = [1 ,2 ,3 ,4 ,5]
    SRS = zero(Float64,1,0)
    return PDSCH, PUSCH, SRS
end
function hMultiULReceive(carrier, SRSs, rx, alg)
    numUEs = length(rx)
    H = [[] for _ in 1:numUEs]
    nVar = [[] for _ in 1:numUEs]

    for ue in 1:numUEs
        srs = SRSs[ue]

        rxGrid = rx[ue].rxGrid
        offset = rx[ue].ChannelFilterDelay

        if isempty(rxGrid)
            rxWaveform = rx[ue].rxWaveform
            rxWaveform = rxWaveform[offset+1:end, :]
            rxGrid = mat"nrOFDMDemodulate($carrier, $rxWaveform)"
        end

        indices, indinfo = mat"nrSRSIndices($carrier, $srs)"
        symbols = mat"nrSRS($carrier, $srs)"

        K = carrier.NSizeGrid * 12
        L = carrier.SymbolsPerSlot
        Nr = size(rxGrid, 3)
        Nt = srs.NumSRSPorts

        urb = unique(collect(transpose(indinfo.PRBSet[:])))
        k = urb * 12 + collect(transpose(1:12))
        k = k[:]

        NCRB = carrier.NStartGrid + carrier.NSizeGrid
        H[ue] = fill(NaN, (NCRB, 1, Nr, Nt))
        nVar[ue] = fill(NaN, (NCRB, 1, Nt))

        nVarRBs = fill(NaN, (length(urb), Nt))

        if alg.PerfectChannelEstimator
            HRBs = mat"nrPerfectChannelEstimate($carrier, $rx[$ue].pathGains, $rx[$ue].pathFilters, $offset, $rx[$ue].sampleTimes)"
            nVarRBs[:] = rx[ue].noisePower
        else
            HRBs = zeros(K, L, Nr, Nt)
            for p in 1:Nt
                Hp, nVarRBs[:, p] = mat"nrChannelEstimate($rxGrid, $indices[:, $p], $symbols[:, $p], 'CDMLengths', $(hSRSCDMLengths(srs)))"
                HRBs[:, :, :, 1:p] = HRBs[:, :, :, 1:p] .+ Hp
            end
        end

        HRBs = HRBs[k, :, :, :]
        HRBs = reshape(HRBs, (12, length(urb), L, Nr, Nt))
        HRBs = permutedims(HRBs, [2, 3, 4, 5, 1])
        HRBs = mean(HRBs, dims = 5)
        HRBs = mean(HRBs, dims = 2)

        H[ue][urb .+ carrier.NStartGrid .+ 1, :, :, :] = HRBs
        nVar[ue][urb .+ carrier.NStartGrid .+ 1, :, :] = nVarRBs
    end

    return H, nVar
end
